package model

import (
	"time"
)

// Host 用户模型
type Host struct {
	HostId        string    `json:"host_id" gorm:"primary_key"`
	Sn            string    `json:"sn" gorm:"size:128"`
	HostName      string    `json:"host_name" gorm:"size:128"`
	Region        string    `json:"region" gorm:"size:128"`
	DataCenter    string    `json:"data_center" gorm:"size:128"`
	RackPosition  string    `json:"rack_position" gorm:"size:128"`
	BmcIp         string    `json:"bmc_ip" gorm:"size:128"`
	AdminIp       string    `json:"admin_ip" gorm:"size:128"`
	BussiIp       string    `json:"bussi_ip" gorm:"size:128"`
	StorIp        string    `json:"stor_ip" gorm:"size:128"`
	PxeMac        string    `json:"pxe_mac" gorm:"size:128"`
	BmcStatus     bool      `json:"bmc_status" gorm:"default:false"`
	RaidStatus    bool      `json:"raid_status" gorm:"default:false"`
	MemosStatus   bool      `json:"memos_status" gorm:"default:false"`
	HostosStatus  bool      `json:"hostos_status" gorm:"default:false"`
	IsJoined      bool      `json:"is_joined" gorm:"default:false"`
	DelFlag       bool      `json:"del_flag" gorm:"defalut:false"`
	CpuCores      string    `json:"cpu_cores" gorm:"size:4"`
	Memory        string    `json:"memory" gorm:"size:8"`
	DiskCount     string    `json:"disk_count" gorm:"size:4"`
	NicCount      string    `json:"nic_count" gorm:"size:4"`
	DiscoveryTime time.Time `json:"discovery_time" gorm:"column:discovery_time"`
	CompleteTime  time.Time `json:"complete_time" gorm:"column:complete_time"`
}

// GetHost 用ID获取host
func GetHost(ID interface{}) (Host, error) {
	var host Host
	result := DB.First(&host, ID)
	return host, result.Error
}

// GetHost 用SN获取host
func GetHostBySn(Sn string) (Host, error) {
	var host Host
	result := DB.Where("Sn = ?", Sn).First(&host)
	return host, result.Error
}

// 创建host
func (host *Host) AddHost() error {
	// 创建用户
	if err := DB.Create(&host).Error; err != nil {
		return err
	}
	return nil
}

func GetHostList(page int, page_size int) (int, []interface{}) {
	var hosts []Host
	// 分页主机列表数据
	hostList := make([]interface{}, 0, len(hosts))
	// 计算偏移量
	offset := (page - 1) * page_size
	// 查询所有的host
	result := DB.Offset(offset).Limit(page_size).Find(&hosts)
	// 查不到数据时
	if result.RowsAffected == 0 {
		return 0, hostList
	}
	// 获取host总数
	total := len(hosts)
	// 查询数据
	result.Offset(offset).Limit(page_size).Find(&hosts)
	//
	for _, useSingle := range hosts {
		hostItemMap := map[string]interface{}{
			"host_id":        useSingle.HostId,
			"sn":             useSingle.Sn,
			"host_mame":      useSingle.HostName,
			"region":         useSingle.Region,
			"data_center":    useSingle.DataCenter,
			"rack_position":  useSingle.RackPosition,
			"bmc_ip":         useSingle.BmcIp,
			"admin_ip":       useSingle.AdminIp,
			"bussi_ip":       useSingle.BussiIp,
			"stor_ip":        useSingle.StorIp,
			"discovery_time": useSingle.DiscoveryTime,
			"complete_time":  useSingle.CompleteTime,
		}
		hostList = append(hostList, hostItemMap)
	}
	return total, hostList
}
